package com.gitee.wsl.io.binary

import com.gitee.wsl.ext.array.readDoubleLE
import com.gitee.wsl.ext.array.readFloatLE
import com.gitee.wsl.ext.array.readInt16LE
import com.gitee.wsl.ext.array.readInt32BE
import com.gitee.wsl.ext.array.readInt32LE
import com.gitee.wsl.ext.array.readLongBE
import com.gitee.wsl.ext.array.readLongLE
import com.gitee.wsl.ext.array.writeDoubleLE
import com.gitee.wsl.ext.array.writeFloatLE
import com.gitee.wsl.ext.array.writeInt16LE
import com.gitee.wsl.ext.array.writeInt32LE
import kotlin.test.Test

class BinaryTest {

    @Test
    fun testFloat() {
        val data = 4.0f
        val toRawBits = data.toRawBits()
        val toBits = data.toBits()
        println("$data,toRawBits:${toRawBits},toBits:${toBits}")
        println("\t fromRawBits:${Float.fromBits(toRawBits)},fromBits:${Float.fromBits(toBits)}")
        val byteArray = ByteArray(Float.SIZE_BYTES)
        byteArray.writeFloatLE(data)
        val rawIntLE = byteArray.readInt32LE()
        val rawIntBE = byteArray.readInt32BE()
        println(" rawIntLE:${rawIntLE},rawIntBE:${rawIntBE}")
        println(" floatLE: ${byteArray.readFloatLE()},fromInt:${Float.fromBits(rawIntLE)}")
    }

    @Test
    fun testDouble() {
        val data = Double.MAX_VALUE
        val toRawBits = data.toRawBits()
        val toBits = data.toBits()
        println("$data,toRawBits:${toRawBits},toBits:${toBits}")
        println("\t fromRawBits:${Double.fromBits(toRawBits)},fromBits:${Double.fromBits(toBits)}")
        val byteArray = ByteArray(Double.SIZE_BYTES)
        byteArray.writeDoubleLE(data)
        val rawIntLE = byteArray.readLongLE()
        val rawIntBE = byteArray.readLongBE()
        val parseValue = byteArray.readDoubleLE()
        println(" rawIntLE:${rawIntLE},rawIntBE:${rawIntBE}")
        println(" floatLE: ${parseValue},fromInt:${Double.fromBits(rawIntLE)}")
        println(" is equ:${parseValue == data}")
    }

    @Test
    fun testBase() {
        val data = ByteArray(FloatData.size)
        FloatData.storeLE(2.0f, data)
        val parsed = FloatData.parse(data)
        println(parsed)
    }

    class SampleData:StructData<SampleData>() {
        val a by int(1)
        val b by long(2)
        val c by byte(3)
        val d by short(4)
        val e by uInt(5u)

        val f by float(12f)
        val g by double(18.0)

    }

    @Test
    fun testStruct() {
        val sample = SampleData()
        println(sample)
        val data = ByteArray(sample.size)
        sample.store(data)
        println("data size :${data.size}  struct size:${sample.size}" )
        sample.parse(data)
        println(sample)
    }

}